from typing import Optional
from fastapi import APIRouter, Query
from apps.schemas.schemas import ProgramCreateRequest, ProgramUpdateRequest
from apps.services.program import (
    get_all_programs,
    get_student_programs_count,
    create_program,
    update_program,
    delete_program
)

programs = APIRouter()


@programs.get("/msg")
async def get_all_programs_api(page: int = Query(1, ge=1), page_size: int = Query(10, ge=1, le=100)):
    """
    获取所有训练项目列表（分页）
    """
    try:
        total, programs_data = await get_all_programs(page, page_size)
        return {
            "status": 200,
            "message": "success",
            "data": programs_data,
            "total": total
        }
    except Exception as e:
        return {
            "status": 500,
            "message": f"Error retrieving programs: {str(e)}",
            "data": [],
            "total": 0
        }


@programs.get("/byStuId/msg/{student_id}")
async def get_student_programs_count_api(student_id: int):
    """
    通过学生ID获取该学生所有学习的科目数量
    """
    try:
        programs_count = await get_student_programs_count(student_id)
        return {
            "status": 200,
            "message": "success",
            "data": programs_count
        }
    except ValueError as e:
        return {
            "status": 404,
            "message": str(e),
            "data": 0
        }
    except Exception as e:
        return {
            "status": 500,
            "message": f"Error retrieving student programs count: {str(e)}",
            "data": 0
        }


@programs.post("/add")
async def create_program_api(data: ProgramCreateRequest):
    """
    创建新的训练项目
    """
    try:
        result = await create_program(data)
        return result
    except ValueError as e:
        return {
            "status": 404,
            "message": str(e),
            "data": {}
        }
    except Exception as e:
        return {
            "status": 500,
            "message": f"Error creating program: {str(e)}",
            "data": {}
        }


@programs.put("/update/{program_id}")
async def update_program_api(program_id: int, data: ProgramUpdateRequest):
    """
    更新训练项目
    """
    try:
        result = await update_program(program_id, data)
        return result
    except ValueError as e:
        return {
            "status": 404,
            "message": str(e),
            "data": {}
        }
    except Exception as e:
        return {
            "status": 500,
            "message": f"Error updating program: {str(e)}",
            "data": {}
        }


@programs.delete("/delete/{program_id}")
async def delete_program_api(program_id: int):
    """
    删除训练项目
    """
    try:
        result = await delete_program(program_id)
        return result
    except ValueError as e:
        return {
            "status": 404,
            "message": str(e),
            "data": {}
        }
    except Exception as e:
        return {
            "status": 500,
            "message": f"Error deleting program: {str(e)}",
            "data": {}
        }
